<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .banner {
            width: 200px;
            height: 300px;
            margin: 40px auto;
            overflow: hidden;
            position: relative;
        }


        .banner ul li {
            width: 200px;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 50px;
            opacity: 0;
            background-color: #ddd;
            position: absolute;
            top: 0;

            transition: .3s;
        }

        span {
            position: absolute;
            z-index: 3;
        }

        ol {
            display: flex;
            position: absolute;
            bottom: 10px;
        }

        ol li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #0f0;
            margin: 10px;
        }

        span {
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 20px;
            height: 20px;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            text-align: center;
            line-height: 20px;
        }

        #next {
            right: 0;
        }


        .show {
            opacity: 1 !important;
        }

        .active {
            background-color: #ff0 !important;
        }
    </style>
</head>

<body>

    <div class="banner" id="a">
        <span id="prev">&lt;</span>
        <span id="next">&gt;</span>
        <ul id="ul">
            <li class="show">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <ol>
            <li class="dot active"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
            <li class="dot"></li>
        </ol>
    </div>

    <script>

        function Banner(ele) {
            // 获取对象
            this.oBanner = document.querySelector('ele');
            this.oPrev = this.oBanner.querySelector('#prev');
            this.oNext = this.oBanner.querySelector('#next');
            this.oUlLis = this.oBanner.querySelectorAll('ul li');
            this.oDots = this.oBanner.querySelectorAll('ol li');

            this.index = 0;
            this.t = null;

            this.autoPlay() ;
            this.init()
        }

        Banner.prototype.init = function() {

            this.oDots.forEach((v,i) => {
                // 对象
                v.index = i ;
            })


            this.oBanner.onclick = (e) => {
                e = e || event ;
                // 上
                if(e.target.id === 'prev') {
                    clearInterval(this.t) ;
                    this.index-- ;
                    if(this.index === -1) {
                        this.index = this.oUlLis.length - 1 ;
                    }
                    this.showOne()
                    return 
                }
                // 下
                if(e.target.id === 'next') {
                    clearInterval(this.t) ;
                    this.index++ ;
                    if(this.index === this.oUlLis.length) {
                        this.index = 0;
                    }
                    this.showOne()
                    return 
                }

                // 小点
                if(e.target.className === 'dot') {
                // if(e.target.classList.contains('dot')) {
                    console.log(1111111);
                    clearInterval(this.t) ;
                    this.index = e.target.index ;
                    this.showOne() 
                }
            }
        }


        Banner.prototype.autoPlay = function() {
            this.t = setInterval(() => {
                this.index++;
                if (this.index === this.oUlLis.length) {
                    this.index = 0;
                }
                // 显示当前这一张  index对应的这一张
                this.showOne()
            }, 1000)
        }

        Banner.prototype.showOne = function() {
             // 清除所有的
             this.oUlLis.forEach((v, i) => {
                v.classList.remove('show');
                this.oDots[i].classList.remove('active');
            })

            this.oUlLis[this.index].classList.add('show');
            this.oDots[this.index].classList.add('active');
        }



        const b = new Banner('#a') ;
        // b.autoPlay()


    </script>

</body>

</html>









